<template>
	<view class="p_b_128">
		<view v-if="listData.length === 0">
			<image src="https://dy.sykj365.com/uploads/mini/zanwudizhi@2x.png" class="p_img_128"></image>
			<view class="p_font_28 p_color_b2 p_flex7">暂无收货地址</view>
		</view>
		<view class="p_over" v-else>
			<view class="list_wrapper" v-for="item in listData" :key="item.id" @click="getItem(item)">
				<view class="p_flex_2">
					<view class="p_font_28 p_one_text"><text class="p_bg_05b570 p_color_fff p_font_24 p_margin_r_8"
							style="line-height: 40rpx;padding: 0 8rpx;border-radius: 4rpx;"
							v-if="item.default == 1">默认</text>{{item.province}}{{item.city}}{{item.area}}{{item.address}}
					</view>
					<view class="p_font_24 p_color_7a p_margin_t_8">{{item.name}}<text
							class="p_margin_l_16">{{(item.phone).replace(/(\d{3})\d{4}(\d{4})/, "$1****$2")}}</text>
					</view>
				</view>
				<navigator catchtap :url="'/pages/my/addAre?id=' + item.id" hover-class="none"
					class="p_tb_8 p_margin_l_100">
					<image src="https://dy.sykj365.com/uploads/mini/bianji@2x.png" class="p_img_32"></image>
				</navigator>
			</view>
			<view class="p_font_24 p_color_7a p_text_c p_b_36" v-if="loadingText">{{loadingText}}</view>
		</view>
		<view class="p_bottom_fix p_bg_fff">
			<navigator :url="'/pages/my/addAre?all=' + listData.length" hover-class="none" class="p_btn">
				<view>新增收货地址</view>
			</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageNum: 1,
				listData: [],
				isLastPage: false, // 是否是最后一页
				loadingText: '',
				isOrder: false, // 是否订单跳转过来
			}
		},
		onLoad(e) {
			if (e.order) this.isOrder = true;
			this.getListData();
		},
		onShow() {
			if (uni.getStorageSync('areaDataUpdate')) {
				this.pageNum = 1;
				this.listData = [];
				this.isLastPage = false;
				this.loadingText = '';

				this.getListData();
				uni.removeStorageSync('areaDataUpdate');
			}
		},
		onReachBottom() {
			if (!this.isLastPage) {
				this.loadingText = '加载中...';
				this.pageNum++;
				this.getListData();
			} else {
				this.loadingText = '已加载全部';
			}
		},
		methods: {
			getListData() {
				this.$http.post(this.$api.addressList, {
					page: this.pageNum
				}).then(res => {
					this.listData = this.listData.concat(res.data.data);

					if (this.pageNum === res.data.last_page) {
						this.isLastPage = true;
						this.loadingText = '已加载全部';
					} else {
						this.isLastPage = false;
					}
				})
			},
			getItem(item) {
				if (!this.isOrder) return
				if (item.province == '新疆维吾尔自治区' || item.province == '西藏自治区') {
					getApp().getMsg('新疆/西藏地区不可选');
					return
				}
				uni.setStorageSync('getAddress', item);
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="less">
	.p_img_128 {
		margin: 328rpx auto 32rpx;
	}

	.list_wrapper {
		display: flex;
		align-items: center;
		margin: 64rpx 0;
		padding: 0 32rpx;
	}
</style>